#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

const static string symbolchars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const static string other="()*+-^ \t\n";
const static string ops="()*+-^";
//ifstream InFile( "detlog.txt" );
/*if( !InFile ) {
cerr << "Couldn´t open input file" << endl;
return false;
}*/

// create reader objects


size_t FindFirstRight(const string& str, size_t pos=0) {
	size_t left=0;
	for (size_t b=str.find_first_of("()",pos); b!=string::npos;
				b=str.find_first_of("()",b+1)	
					){
			if (b!=string::npos) {
			if (str[b]=='(')
				left+=1;
			else {
				if (left==0) 
						return b;
				else left-=1;
			}
			}
			//else throw("found non paired parenthese!");
			else return b;
	}
}

size_t FindPlusPivot(const string& str){
	size_t b=str.find_first_of(other);
	while (b!=string::npos){
		switch(str[b]){
			case ')':
					throw("invalid input!");
			case '+':
			return b;
			case '-':
				if (b!=0) {
						return b;
						break;
				}
				/*else 
						b=str.find_first_of(other,b+1);
						*/
			case '(':
					b=FindFirstRight(str,b+1);
			default:
					b=str.find_first_of(other,b+1);
		}
	}
	return b;		
}

size_t FindTimesPivot(const string& str){
	size_t b=str.find_first_of(other);
	while (b!=string::npos){
		switch(str[b]){
			case ')':
					throw("invalid input!");
			case '+':
			return b;
			case '-':
				if (b!=0) {
						return b;
						break;
				}
			case '(':
					b=FindFirstRight(str,b+1);
			default:
					b=str.find_first_of(other,b+1);
		}
	}
	return b;		
}

vector<size_t> Skeleton(const string& str) {
	vector<size_t> stn;
	size_t pre=0;
	size_t b=str.find_first_of(ops);
	while (b!=string::npos){
		if (str[b]=='(')
			b=FindFirstRight(str,b+1);
		else 
		{stn.insert(stn.end(),b-pre);
			pre=b;
		}
		b=str.find_first_of(ops,b+1);
	}
	return stn;
}

/*
void Skeleton(const string& str, vector<size_t>& stn) {
	//vector<size_t> *stn =new vector<size_t>;
	size_t pre=0;
	size_t b=str.find_first_of(ops);
	while (b!=string::npos){
		if (str[b]=='(')
			b=FindFirstRight(str,b+1);
		else 
		{stn.insert(stn.end(),b-pre);
			pre=b;
		}
		b=str.find_first_of(ops,b+1);
	}
	//return stn;
}
*/
void myprint (size_t i) {

		cout << " " << i;
}


int main () {
		cout << "Input the string, and I will found the plus/minus pivot of it:\n";
		string str;
		/*istream_iterator<string> DataBegin( cin );
		istream_iterator<string> DataEnd;

		while( DataBegin != DataEnd ) {
		str += *DataBegin;
		DataBegin++;
		} */
		string line;
		//std::ifstream in("bla");
		while(getline(cin,line))
				str += line;
		/*string str;
		 cin >> str;
		 */
		cout << "You inputed (after trailing newlines) "<<str <<endl;
		size_t l=str.find_first_of(other);
		if (l==string::npos) 
				cout << "no nontivial plus/minus pivot\n";
		else 
				cout << "the pivot is at position: " << FindPlusPivot(str) <<endl;
		vector<size_t> stn = Skeleton(str);
		for_each(stn.begin(),stn.end(),myprint);
		cout << endl;
		//delete stn;

		return 0;
}
